package client_gui_chat;

import utils.Constants;
import client.ClientApplication;

/**
 * Thread koji kontrolise slanje protokolskih poruka o pisanju poruke.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class TypingThread extends Thread {

	private boolean running = true;
	private static final int SLEEP_TIME = 1000;
	
	private ClientApplication theApp;
	private ChatDialog chatDialog;
	
	private boolean typing = false;
	private boolean tempTyping = typing;
	
	public TypingThread(ClientApplication theApp, ChatDialog dialog) {
		this.theApp = theApp;
		this.chatDialog = dialog;
	}
	
	
	@Override
	public void run() {

		while ( running ) {
			
			try {
				Thread.sleep(SLEEP_TIME);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			// salje se komanda jedino ako se promenio status kucanja poruke
			if ( typing != tempTyping ) {
				tempTyping = typing;
				String parameter = theApp.getClientUser().getUsername() + " " + typing;
				
//				PEER_TYPINGCommand ptCommand = new PEER_TYPINGCommand(parameter);
//				ptCommand.executeCommand(theApp);
				
				if ( chatDialog != null && chatDialog.getConnection() != null )
					// TODO koriscen NO_BUFFERING
//					chatDialog.getConnection().setMessage(Constants.NO_BUFFERING, "PEER_TYPING " + parameter);
					chatDialog.getConnection().writeData("PEER_TYPING " + parameter);
			}
		}
	}
	
	
	public void setTyping(boolean typing) {
		this.typing = typing;
	}
	
	public void setRunning(boolean flag) {
		running = flag;
	}
	
	public boolean isRunning() {
		return running;
	}
}
